# # -*- coding:utf-8 -*-
import sqlconnect

def deletewifidevice(mac):
    db = sqlconnect.connect()
    cursor = db.cursor()
    sql = "delete from wifiDevice where asset_mac = %s"
    cursor.execute(sql, [mac])
    db.commit()
    print "delete", mac
    db.close()

def deletenotAStation(mac):
    db = sqlconnect.connect()
    cursor = db.cursor()
    sql = "delete from not_A_Station where mac = %s"
    cursor.execute(sql, [mac])
    db.commit()
    print "delete", mac
    db.close()

def deletewifiprobSsid(name):
    db = sqlconnect.connect()
    cursor = db.cursor()
    sql = "delete from wifi_probSsid where wifi_name = %s"
    cursor.execute(sql, [name])
    db.commit()
    print "delete", name
    db.close()

def deleteAassication(mac):
    db = sqlconnect.connect()
    cursor = db.cursor()
    sql = "delete from wifi_A_Station  where AClient = %s"
    cursor.execute(sql, [mac.lower()])
    db.commit()
    print "delete", mac
    db.close()

def changewifiDeviceStatus():
    db = sqlconnect.connect()
    cursor = db.cursor()
    sql = "update wifiDevice set status=0;"
    cursor.execute(sql)
    db.commit()
    db.close()
# 查询出wifiDoss 对应Ap的doss攻击总数
def wifiDosscount(mac):
    db = sqlconnect.connect()
    cur = db.cursor()
    sql = " select count(*) from wifi_Doss where victim = %s"
    # print sql
    cur.execute(sql,[mac])
    count = cur.fetchall()
   # print count
    db.close()
    return count
# 查询Ap_name对应的伪ap信息
def wifiFakeAp(name):
    db = sqlconnect.connect()
    cur = db.cursor()
    sql = "select fake_M,create_time,fakename from wifi_fakeap where fakename = %s order by create_time desc limit 1"
    # print sql
    cur.execute(sql, [name])
    count = cur.fetchall()
  #  print count
    db.close()
    return count
# 当获取到新的AP连接信息之后，清空数据表
def deletefromAStation():
    db = sqlconnect.connect()
    cursor = db.cursor()
    sql = "truncate table wifi_A_Station"
    cursor.execute(sql)
    db.commit()
    print "clear the table wifi_A_Station"
    db.close()
# 清空之前的未连接Wi-Fi的信息
def deletefromNotAStation():
    db = sqlconnect.connect()
    cursor = db.cursor()
    sql = "delete from not_A_Station"
    cursor.execute(sql)
    db.commit()
    print "clear the table not_A_Station"
    db.close()
# 根据AP名字找到探测请求攻击
def findProbeReqAtkByApName(macName):
    db = sqlconnect.connect()
    cur = db.cursor()
    sql = " select count(*) from wifi_Doss where Dos_type = 'proberequAttack' and victim = %s"
    # print sql
    cur.execute(sql,[macName])
    count = cur.fetchall()
    if macName == 'leyard':
        print 'ssssss',count
    elif macName == 'Tenda_F88':
        print ''
    db.close()
    return count
def finBeaconAttack():
    db = sqlconnect.connect()
    cursor = db.cursor()
    sql = "select id, Dos_type, create_time, victim from wifi_Doss where Dos_type = 'BeaconFloodAttack' order by create_time desc"
    cursor.execute(sql)
    count = cursor.fetchall()
    db.close()
    return count
def findBeaconAttackfromwifiDoss():
    db = sqlconnect.connect()
    cursor = db.cursor()
    sql = "select count(*) from wifi_Doss where Dos_type = 'BeaconFloodAttack'"
    cursor.execute(sql)
    count = cursor.fetchall()
    db.close()
    return count

# 在获取当前环境中是否存在Beacon攻击的时候，清除之前的beacon
def deleteBeaconAttackfromwifiDoss():
    db = sqlconnect.connect()
    cursor = db.cursor()
    sql = "delete from wifi_Doss where Dos_type='BeaconFloodAttack'"
    cursor.execute(sql)
    db.commit()
    print "clear the BeaconAttack"
    db.close()
def deleterouterleakByasset_mac(mac):
    db = sqlconnect.connect()
    cursor = db.cursor()
    sql = "delete from router_leak where asset_mac=%s"
    cursor.execute(sql,[mac])
    db.commit()
    print "delete router_leak",mac
    db.close()

def getmessageFronRouterLibraryByName(routerleak):
    db = sqlconnect.connect()
    cursor = db.cursor()
    sql = "select leak_name,rank,description,leakdevice from routerlibrary where routerleak='%s'" %(routerleak)
    cursor.execute(sql)
    s = cursor.fetchall()
    db.close()
    return s

def wifiDossDic(atta):
    a={'associationAttack':'关联攻击',
       'DisassociationAttack':'请求关联攻击',
       'DataReplyAttack':'QoS数据重放攻击',
       "proberequAttack":'探测请求攻击',
       'authAttackdic':'Auth攻击',
       'DeauthAttack':'Deauth攻击',
       'BeaconFloodAttack':'Beancon泛洪攻击',
       'bleFuzzAttack':'Fuzz攻击',
       'bleConnectAttack':'连接攻击',
       'bleAdAttack':'广播攻击',
       'Fuzz':'Fuzz攻击'
       }
    return a[atta]
if __name__ == '__main__':
    print findBeaconAttackfromwifiDoss()